const db = wx.cloud.database();
const productsCollection = db.collection('products');
const limit = 10 ;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabBar:0,            //用来判断是否隐藏tabbar
    scrollTop:0,         // 设置初始滚动位置
    productlike:[],
    productRec:[],
    productNew:[],
    product:[],
    tota:0,
    page:1,
    pagenew:2,
    pagenew2:2,
    pageprice:1,
    activeSwiperIndex:0,    // activeSwiperIndex，activeTabIndex 绑定联动 是首页 推荐等
    activeTabIndex:0,         // 让滑动换面与点击 首页和推荐等绑定
    activeProductTabIndex:0,   //是商品中的 综合， 销量， 新品 价格
  },
  async addMore(){
    // let{data} = productsCollection.get();
    // wx.showLoading({
    //   title: '正在加载中....'
    // })
    wx.showLoading({
      title: '正在加载中....'
    })
    let { data } = await productsCollection
      .skip(this.data.page * limit)
      .limit(limit)
      .get();
    wx.hideLoading();
    this.setData({
      product:[...this.data.product,...data],
      page:++this.data.page 
    })
   
    if(this.data.product.length >= this.data.total){
      wx.showToast({
        title: '没有更多商品了....',
      })
      return;
    }
    

  },
  async addMoreNew(){
    wx.showLoading({
      title: '正在加载中....'
    })
    
    let { data } = await productsCollection
    .where({
      isNew: db.command.eq(true)
    })
    .skip(this.data.pagenew * limit)
    .limit(limit)
    .get();
    wx.hideLoading();
    this.setData({
      productNew:[...this.data.productNew,...data],
      pagenew:++this.data.pagenew
    })
    
  },
 
  goToDetail(e){
   if(e.currentTarget.dataset.item.isShoes){
      wx.navigateTo({
        url: `../detail/detail?id=${e.currentTarget.dataset.item.id}`
      })
   }
   else{
      wx.navigateTo({
        url: `../detail2/detail2?id=${e.currentTarget.dataset.item.id}`,
      })
   }
  },
  async priceinit(){
    let{data} = await productsCollection()
    .orderBy("price","asc")
    .limit(limit)
    .get()
    this.setData({
      pageprice:1,
      product:data
    })
  },
  async init(){
    let {data} = await productsCollection()
    .limit(limit)
    .get();
    this.setData({
      page:1,
      product:data
    })
  },
  async newinit(){
    let {data} = await productsCollection
    .where({
      isNew: db.command.eq(true)
    })
    .limit(limit)
    .get();
    this.setData({
      pagenew:1,
      product:data,
    })
  },
  async isNewEnd(){
    let {total} = await productsCollection
    .where({
      isNew: db.command.eq(true)
    })
    .count();
    if(product.length>=total){
      wx.showToast({
        title: '正在加载中....',
      })
      return;
    }
  },
  async addMore_product(){
    wx.showLoading({
      title: '正在加载中....'
    })
    if(this.data.activeProductTabIndex==2){
     
      let {data} = await productsCollection
      .where({
        isNew: db.command.eq(true)
      })
      .skip(this.data.pagenew*limit)
      .limit(limit)
      .get()
      wx.hideLoading();
      this.setData({
        product:[...this.data.product,...data],
        pagenew:++this.data.pagenew
      })
      isNewEnd();
    }
    if(this.data.activeProductTabIndex==3){
      let {data} =await productsCollection
      .orderBy("price","asc")
      .skip(this.data.pageprice *limit)
      .limit(limit)
      .get()
      wx.hideLoading();
      this.setData({
        product:[...this.data.product,...data],
        pageprice:++this.data.pageprice
      })
      
    }
    else{
      let { data } = await productsCollection
      .skip(this.data.page * limit)
      .limit(limit)
      .get();
      
      wx.hideLoading();
      this.setData({
        product:[...this.data.product,...data],
        page:++this.data.page 
      })

      if(this.data.product.length >= this.data.total){
        wx.showToast({
          title: "正在加载中",
        })
        return;
      }
    }
  },

  doListChange(e){
    let index=e.detail.current;
    this.setData({
      activeTabIndex:index,
    })
  },
  async switchProductTabnew(e){
    let {total} = await productsCollection
    .where({
      isNew: db.command.eq(true)
    })
    .count();
    let index=e.currentTarget.dataset.index;
    if(index == this.data.activeProductTabIndex){
      return ;
    }
    this.newinit();
    this.setData({
      activeProductTabIndex:index,
    })
  },
  async switchProductTab(e){
    let index=e.currentTarget.dataset.index;
    let {data} = await productsCollection.get()
    if(index == this.data.activeProductTabIndex){
      return ;
    }
    this.setData({
      activeProductTabIndex:index,
      page:0,
      product:data,
    })
  },

  async switchProductTabprice(e){
    let index=e.currentTarget.dataset.index;
    if(index == this.data.activeProductTabIndex){
      return ;
    }
    let {data} =await productsCollection
    .orderBy("price","asc")
    .skip(0)
    .limit(limit)
    .get()
    this.setData({
      activeProductTabIndex:index,
      pageprice:1,
      product:data
    })
    // let {data} =await productsCollection
    // .orderBy("price","asc")
    // .skip(this.data.pageprice *limit)
    // .limit(limit)
    // .get()
  },
  switchTabproduct(e){
    let index=e.currentTarget.dataset.index;
    if(index == this.data.activeTabIndex){
      return ;
    }
    this.setData({
      activeTabIndex:index,
    })
    let flag=this.data.activeProductTabIndex;
    if(flag==2){
      this.newinit();
    }
    if(flag==3){
      this.priceinit();
    }
    else{
      this.init();
    }
  },
  switchTab(e){
    let index=e.currentTarget.dataset.index;
    if(index == this.data.activeTabIndex){
      return ;
    }
    this.setData({
      activeTabIndex:index,
    })
  },
  switchTabNew(e){
    this.newinit();
    let index=e.currentTarget.dataset.index;
    if(index == this.data.activeTabIndex){
      return ;
    }

    this.setData({
      activeTabIndex:index,
    })
  },
  goToSearch(){
    wx.navigateTo({
      url: '../search/search',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(){
    let { data } = await productsCollection.skip(0).limit(limit).get();
    let {data:datanew} = await productsCollection.where({
      isNew:true
    }).limit(limit).get();
    let productlike=[];
    let productrec=[];
    let productnew=[];
    let {total} = await productsCollection.count()
    data.forEach(productlist =>{
      if(productlist.isTitle==true){
        productlike.push(productlist)
      }
      if(productlist.isRec==true){
        productrec.push(productlist)
      }
    })
    datanew.forEach(productlist=>{
        if(productlist.isNew==true){
          productnew.push(productlist)
        }
    })
    this.setData({
      productlike:productlike,
      product:data,
      productRec:productrec,
      productNew:productnew,
      total:total,
    })
    console.log(this.data.productNew);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})